/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Last Change:
 *    by:   $Author: $
 *    date: $Date:   $
 * Copyright (c): Johannes Kuhaupt
 * Licence: Creative Commons CC BY-NC-SA 3.0
 */
package view;

import control.GuiManager;
import control.SettingsManager;
import java.awt.Font;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import say.swing.JFontChooser;

/**
 *
 * @author Johannes Kuhaupt
 */
public class OptionsDialog extends javax.swing.JDialog {

    say.swing.JFontChooser font;
    GuiManager guiMan;
    final JFileChooser fc;
    MainFrame parent;
    
    
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();
    
    private String[] cmboxColorScheme = { langBundle.getString("BooEditorOptionsCmboColorLight"), langBundle.getString("BooEditorOptionsCmboColorDark")};
    private String[] cmboxTabsOrSpaces = { langBundle.getString("BooEditorOptionsCmboIndentationTab"), langBundle.getString("BooEditorOptionsCmboIndentationSpaces")};
    /**
     * Creates new form OptionDialog
     * @param parent 
     * @param modal
     * @param pGuiMan  
     */
    public OptionsDialog(java.awt.Frame pParent, boolean modal, GuiManager pGuiMan) {
        super(pParent, modal);
        parent = (MainFrame)pParent;
        initComponents();
        this.guiMan = pGuiMan;
        fc = new JFileChooser();
        this.setLocationRelativeTo(getParent());
        font = new say.swing.JFontChooser();
        this.readConfigAndUpdateView();
        //this.guiMan.updateGui();
        this.setResizable(false);
        this.setVisible(true);
        
    }
    
    
    
    private void readConfigAndUpdateView(){
        this.spinner_bigScreenFontSize.setValue(SettingsManager.getIntSettingValue("FONTSIZEBIGSCREEN"));
        this.check_LineNumbers.setSelected(SettingsManager.getBooleanSettingValue("LINENUMBERS"));
        this.check_toolbarRight.setSelected(SettingsManager.getBooleanSettingValue("ShowToolbarRight"));
        this.check_Antialias.setSelected(SettingsManager.getBooleanSettingValue("ANTIALIAS"));
        this.check_HighlightActiveLine.setSelected(SettingsManager.getBooleanSettingValue("HIGHLIGHTACTIVELINE"));
        this.check_ShowSpaces.setSelected(SettingsManager.getBooleanSettingValue("SHOWSPACES"));
        
        Font f = new Font(SettingsManager.getStringSettingValue("FONTNAME"),SettingsManager.getIntSettingValue("FONTSTYLE"), SettingsManager.getIntSettingValue("FONTSIZE") );
        font.setSelectedFont(f);
        
        
        if(SettingsManager.getBooleanSettingValue("INDENTATIONBYTAB")){
            this.cmbo_tabsOrSpaces.setSelectedIndex(0);
        }
        else{
            this.cmbo_tabsOrSpaces.setSelectedIndex(1);
        }
        
        if(SettingsManager.getBooleanSettingValue("ShowToolbarRight")){
            this.parent.showToolbarRight();
        }
        else{
            this.parent.hideToolbarRight();
        }
        
        this.spin_tabSize.setValue(SettingsManager.getIntSettingValue("TABSIZE"));
        
        this.check_syntaxHighlighting.setSelected(SettingsManager.getBooleanSettingValue("SYNTAXHIGHLIGHTING"));
        
        if(SettingsManager.getStringSettingValue("HIGHLIGHTINGTHEME").equals("default")){
            this.cmbo_colorMode.setSelectedIndex(0);
        }
        else{
            this.cmbo_colorMode.setSelectedIndex(1);
        }
        
        this.check_autoIndentation.setSelected(SettingsManager.getBooleanSettingValue("AUTOINDENTATION"));
        
        
        //this.jCheckHightlightBraces.setSelected(SettingsManager.getBooleanSettingValue("HIGHLIGHTBRACES"));
        //this.jCheckBracesComp.setSelected(SettingsManager.getBooleanSettingValue("AUTOBRACES"));
        
        
       
        
        
    }
    
    
    private void saveEditorConfig(){
        SettingsManager.updateSettingsItem("LINENUMBERS", this.check_LineNumbers.isSelected()+"");
        SettingsManager.updateSettingsItem("ShowToolbarRight", this.check_toolbarRight.isSelected()+"");
        SettingsManager.updateSettingsItem("ANTIALIAS", this.check_Antialias.isSelected()+"");
        SettingsManager.updateSettingsItem("HIGHLIGHTACTIVELINE", this.check_HighlightActiveLine.isSelected()+"");
        SettingsManager.updateSettingsItem("SHOWSPACES", this.check_ShowSpaces.isSelected()+"");
        
        SettingsManager.updateSettingsItem("FONTSIZEBIGSCREEN", this.spinner_bigScreenFontSize.getValue()+"");
        
        
        if(SettingsManager.getBooleanSettingValue("ShowToolbarRight")){
            this.parent.showToolbarRight();
        }
        else{
            this.parent.hideToolbarRight();
        }
        
         if( this.cmbo_tabsOrSpaces.getSelectedIndex()==0 ){
            SettingsManager.updateSettingsItem("INDENTATIONBYTAB", "true");
         }
         else{
             SettingsManager.updateSettingsItem("INDENTATIONBYTAB", "false");
         }
         
         SettingsManager.updateSettingsItem("TABSIZE", this.spin_tabSize.getValue()+"");
         
         SettingsManager.updateSettingsItem("SYNTAXHIGHLIGHTING", this.check_syntaxHighlighting.isSelected()+"");
        if(this.cmbo_colorMode.getSelectedIndex()==0){
            SettingsManager.updateSettingsItem("HIGHLIGHTINGTHEME", "default");
        }
        else{
            SettingsManager.updateSettingsItem("HIGHLIGHTINGTHEME", "dark");
        }
        
         
        
        SettingsManager.updateSettingsItem("AUTOINDENTATION", this.check_autoIndentation.isSelected()+"");
        
        
        
        //SettingsManager.updateSettingsItem("HIGHLIGHTBRACES", this.jCheckHightlightBraces.isSelected() +"");
        //SettingsManager.updateSettingsItem("AUTOBRACES", this.jCheckBracesComp.isSelected()+"");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        check_ShowSpaces = new javax.swing.JCheckBox();
        check_HighlightActiveLine = new javax.swing.JCheckBox();
        check_Antialias = new javax.swing.JCheckBox();
        check_syntaxHighlighting = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        lbl_colorMode = new javax.swing.JLabel();
        cmbo_colorMode = new JComboBox(cmboxColorScheme);
        jPanel3 = new javax.swing.JPanel();
        lbl_font = new javax.swing.JLabel();
        btn_fontChooser = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        lbl_bigScreenFontSize = new javax.swing.JLabel();
        spinner_bigScreenFontSize = new javax.swing.JSpinner();
        check_autoIndentation = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        lbl_tabsOrSpaces = new javax.swing.JLabel();
        cmbo_tabsOrSpaces = new JComboBox(cmboxTabsOrSpaces);
        jPanel5 = new javax.swing.JPanel();
        lbl_tabsize = new javax.swing.JLabel();
        spin_tabSize = new javax.swing.JSpinner();
        check_LineNumbers = new javax.swing.JCheckBox();
        check_toolbarRight = new javax.swing.JCheckBox();
        jPanel7 = new javax.swing.JPanel();
        btn_close = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(langBundle.getString("BooEditorOptionsFrameTitle"));
        setMinimumSize(new java.awt.Dimension(350, 300));

        jPanel1.setMaximumSize(new java.awt.Dimension(600, 600));
        jPanel1.setMinimumSize(new java.awt.Dimension(200, 46));
        jPanel1.setLayout(new java.awt.GridLayout(13, 1, 20, 5));

        check_ShowSpaces.setText(langBundle.getString("BooEditorOptionsShowSpaces"));
        jPanel1.add(check_ShowSpaces);

        check_HighlightActiveLine.setText(langBundle.getString("BooEditorOptionsHighlightActiveLive"));
        jPanel1.add(check_HighlightActiveLine);

        check_Antialias.setText(langBundle.getString("BooEditorOptionsAntiAlias"));
        jPanel1.add(check_Antialias);

        check_syntaxHighlighting.setText(langBundle.getString("BooEditorOptionsSyntaxHighlighting"));
        jPanel1.add(check_syntaxHighlighting);

        java.awt.FlowLayout flowLayout1 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0);
        flowLayout1.setAlignOnBaseline(true);
        jPanel2.setLayout(flowLayout1);

        lbl_colorMode.setLabelFor(cmbo_colorMode);
        lbl_colorMode.setText(langBundle.getString("BooEditorOptionsLblColor"));
        jPanel2.add(lbl_colorMode);

        cmbo_colorMode.setMaximumSize(new java.awt.Dimension(150, 40));
        jPanel2.add(cmbo_colorMode);

        jPanel1.add(jPanel2);

        java.awt.FlowLayout flowLayout2 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0);
        flowLayout2.setAlignOnBaseline(true);
        jPanel3.setLayout(flowLayout2);

        lbl_font.setLabelFor(btn_fontChooser);
        lbl_font.setText(langBundle.getString("BooEditorOptionsLblFont"));
        jPanel3.add(lbl_font);

        btn_fontChooser.setText(langBundle.getString("BooEditorOptionsBtnChooseFont"));
        btn_fontChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_fontChooserActionPerformed(evt);
            }
        });
        jPanel3.add(btn_fontChooser);

        jPanel1.add(jPanel3);

        java.awt.FlowLayout flowLayout3 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0);
        flowLayout3.setAlignOnBaseline(true);
        jPanel8.setLayout(flowLayout3);

        lbl_bigScreenFontSize.setText(langBundle.getString("BooOptionsLblBigScreen"));
        jPanel8.add(lbl_bigScreenFontSize);
        jPanel8.add(spinner_bigScreenFontSize);

        jPanel1.add(jPanel8);

        check_autoIndentation.setText(langBundle.getString("BooEditorOptionsAutoIndentation"));
        jPanel1.add(check_autoIndentation);

        java.awt.FlowLayout flowLayout4 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0);
        flowLayout4.setAlignOnBaseline(true);
        jPanel4.setLayout(flowLayout4);

        lbl_tabsOrSpaces.setLabelFor(cmbo_tabsOrSpaces);
        lbl_tabsOrSpaces.setText(langBundle.getString("BooEditorOptionsLblIndentation"));
        jPanel4.add(lbl_tabsOrSpaces);

        cmbo_tabsOrSpaces.setMaximumSize(new java.awt.Dimension(100, 30));
        jPanel4.add(cmbo_tabsOrSpaces);

        jPanel1.add(jPanel4);

        java.awt.FlowLayout flowLayout5 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0);
        flowLayout5.setAlignOnBaseline(true);
        jPanel5.setLayout(flowLayout5);

        lbl_tabsize.setLabelFor(spin_tabSize);
        lbl_tabsize.setText(langBundle.getString("BooEditorOptionsLblTabSize"));
        jPanel5.add(lbl_tabsize);

        spin_tabSize.setMaximumSize(new java.awt.Dimension(100, 30));
        spin_tabSize.setMinimumSize(new java.awt.Dimension(29, 10));
        jPanel5.add(spin_tabSize);

        jPanel1.add(jPanel5);

        check_LineNumbers.setText(langBundle.getString("BooEditorOptionsShowLineNumbers"));
        jPanel1.add(check_LineNumbers);

        check_toolbarRight.setText(langBundle.getString("BooEditorOptionsShowToolbarRight"));
        jPanel1.add(check_toolbarRight);

        btn_close.setText(langBundle.getString("BooEditorOptionsBtnClose"));
        btn_close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_closeActionPerformed(evt);
            }
        });
        jPanel7.add(btn_close);

        jPanel1.add(jPanel7);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_closeActionPerformed
        saveEditorConfig();
        this.guiMan.updateGui();
        this.dispose();
    }//GEN-LAST:event_btn_closeActionPerformed

    private void btn_fontChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_fontChooserActionPerformed

        int returnVal = font.showDialog(this);
        if(returnVal==JFontChooser.OK_OPTION){
            Font f = font.getSelectedFont();
            SettingsManager.updateSettingsItem("FONTNAME", f.getName());
            SettingsManager.updateSettingsItem("FONTSIZE", f.getSize()+"");
            SettingsManager.updateSettingsItem("FONTSTYLE", f.getStyle()+"");

        }

    }//GEN-LAST:event_btn_fontChooserActionPerformed

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_close;
    private javax.swing.JButton btn_fontChooser;
    private javax.swing.JCheckBox check_Antialias;
    private javax.swing.JCheckBox check_HighlightActiveLine;
    private javax.swing.JCheckBox check_LineNumbers;
    private javax.swing.JCheckBox check_ShowSpaces;
    private javax.swing.JCheckBox check_autoIndentation;
    private javax.swing.JCheckBox check_syntaxHighlighting;
    private javax.swing.JCheckBox check_toolbarRight;
    private javax.swing.JComboBox cmbo_colorMode;
    private javax.swing.JComboBox cmbo_tabsOrSpaces;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JLabel lbl_bigScreenFontSize;
    private javax.swing.JLabel lbl_colorMode;
    private javax.swing.JLabel lbl_font;
    private javax.swing.JLabel lbl_tabsOrSpaces;
    private javax.swing.JLabel lbl_tabsize;
    private javax.swing.JSpinner spin_tabSize;
    private javax.swing.JSpinner spinner_bigScreenFontSize;
    // End of variables declaration//GEN-END:variables
}
